$(function () {

  console.log(getCookie('accounts'))
  checkCookie()
  $.ajaxSetup({
    async: false,
    timeout: 15 * 1000,
    xhrFields: {
      withCredentials: true
    },
    error: function () {
      layer.open($.extend(true, { content: '网络链接错误' }, layOptions));
    }
  })
  $('#sub').on('click', function (event) {
    event.preventDefault();
    var bol = checkout()
    // if (bol) {
    $.get(urlStr + '/logon.cspx', {
      accounts: $('#username').val(),
      pwd: $('#password').val(),
    }, function (res) {
      var json = $.parseJSON(res)
      if (!json.Success) {
        layer.open({
          content: json.Content,
          btn: '好的',
          yes: function (index) {
            layer.close(index)
          }
        });
      } else {
        layer.open({
          content: '登陆成功',
          btn: '好的',
          yes: function (index) {
            layer.close(index)
            setCookie('accounts', $('#username').val(), 1)
            window.location.reload()
          }
        });

      }
    });
    // }
  });

  function checkout() {
    var user = [{
      checkVal: $('#password').val(),
      error: '请输入用户密码',
      dom: $('#password')
    }, {
      checkVal: $('#username').val(),
      error: '请输入用户名',
      dom: $('#username')
    }]

    user.forEach(function (ele, ind) {
      if (!ele.checkVal) {
        console.log(ind)
        layer.open({
          content: ele.error,
          btn: '好的',
          yes: function (index) {
            layer.close(index)
            ele.dom.focus()
            // return
          }
        });
        return false
      }
      return true
    })
  }
});



